package cn.shengongying.api.controller;

import cn.shengongying.anon.Ignore;
import cn.shengongying.api.dto.LogInDTO;
import cn.shengongying.bean.BaseUser;
import cn.shengongying.bean.ResponseResult;
import cn.shengongying.enums.CacheKeyEnum;
import cn.shengongying.enums.ResponseCodeEnum;
import cn.shengongying.exception.DigestException;
import cn.shengongying.model.Customer;
import cn.shengongying.service.ICustomerService;
import cn.shengongying.utils.GeTuiUtils;
import cn.shengongying.utils.JwtUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * author: RXK
 * date: 2022/1/6 星期四
 * desc:
 */
@Api(tags="登录")
@RestController
@RequestMapping("/boss/login")
public class LoginController extends BaseController {

    private final ICustomerService customerService;


    private final RedisTemplate<String, String> redisTemplate;

    public LoginController(ICustomerService customerService, RedisTemplate<String, String> redisTemplate) {
        this.customerService = customerService;
        this.redisTemplate = redisTemplate;
    }


    @Ignore
    @PostMapping
    @ApiOperation(value = "一键登录")
    public ResponseResult<String> phone(@RequestBody LogInDTO dto) {
        dto.checkParam();
        switch (dto.getType()) {
            case 0:
                return LogInPhoneAndToken(dto);
            case 1:
                return LoginPhoneAndCode(dto);
            default:
                return ResponseResult.fail();
        }
    }



    private ResponseResult<String> handleByPhone(String phone) {
        Customer customer = customerService.login(phone);
        BaseUser user = customer.buildBaseUser();
        String token;
        try {
            token = JwtUtils.token(user);
        } catch (DigestException e) {
            return ResponseResult.fail(ResponseCodeEnum.server_exception);
        }
        return ResponseResult.success(token);
    }

    /**
     * 通过个推的手机号码一键登录认证 登录的方式
     */
    private ResponseResult<String> LogInPhoneAndToken(LogInDTO dto) {
        String phone = "13560184921";
        try {
            phone = GeTuiUtils.gy_get_pn(dto.getCode(), dto.getGyuid());
        } catch (Exception e) {
            logger.error("获取手机号出现异常：", e);
            return ResponseResult.fail(ResponseCodeEnum.server_exception);
        }
        return handleByPhone(phone);
    }

    private ResponseResult<String> LoginPhoneAndCode(LogInDTO dto) {
        String key = CacheKeyEnum.phone.getKey(dto.getPhone());
        String value = redisTemplate.opsForValue()
                .get(key);
        if (StringUtils.isEmpty(value) || !value.equalsIgnoreCase(dto.getCode())) {
            return ResponseResult.fail(ResponseCodeEnum.verify_code_expire_exception);
        }
        redisTemplate.delete(key);
        return handleByPhone(dto.getPhone());
    }


}
